package com.fulei.logic;

import android.annotation.SuppressLint;
import com.bjfl.saleManager.util.LogUtil;
import com.dwin.navy.serialportapi.SerailPortOpt;

import java.util.List;

@SuppressLint({"SdCardPath"})
public class PrintOperate
{
  private static SerailPortOpt serialPort = null;
  public static String comNo = "";
  public static String baudRate = "";
  
  public static boolean OpenPort()
  {
    try
    {
      serialPort = new SerailPortOpt();
      
      serialPort.mDevNum = Integer.parseInt(comNo);
      serialPort.mSpeed = Integer.parseInt(baudRate);
      serialPort.mDataBits = 8;
      serialPort.mStopBits = 1;
      serialPort.mParity = 110;
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
    return true;
  }
  
  public static void closeSerialPort()
  {
    try
    {
      if (serialPort.mFd != null) {
        serialPort.closeDev(serialPort.mFd);
      }
      serialPort.mFd = null;
      serialPort = null;
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
  }
  
  public static int printLuckyNumInfo(List<String> prints)
  {
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] PrintInfoData = { 10 };
      
      String[] storeStr = (String[])prints.toArray(new String[prints.size()]);
      for (String s : storeStr)
      {
        System.out.println(s);
        byte[] bSou = s.getBytes("GB18030");
        serialPort.writeBytes(bSou);
      }
      if (null != serialPort.mFd) {
        serialPort.writeBytes(PrintInfoData);
      }
      Thread.sleep(1000L);
      


      byte[] PrintAlignmentData = { 27, 97, 1 };
      if (null != serialPort.mFd) {
        serialPort.writeBytes(PrintAlignmentData);
      }
      byte[] PrintPaperData = { 27, 100, 8 };
      if (null != serialPort.mFd) {
        serialPort.writeBytes(PrintPaperData);
      }
      byte[] cutPaperData = { 27, 105 };
      if (null != serialPort.mFd) {
        serialPort.writeBytes(cutPaperData);
      }
    }
    catch (Exception ex)
    {
      LogUtil.writePrintInfo("打印内容异常:" + ex.getMessage());
      return -1;
    }
    return 0;
  }
  
  public static int printPrizeInfo(List<String> prints)
  {
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] PrintInfoData = { 10 };
      
      String[] storeStr = (String[])prints.toArray(new String[prints.size()]);
      for (String s : storeStr)
      {
        System.out.println(s);
        byte[] bSou = s.getBytes("GB18030");
        serialPort.writeBytes(bSou);
      }
      if (null != serialPort.mFd) {
        serialPort.writeBytes(PrintInfoData);
      }
      byte[] PrintAlignmentData = { 27, 97, 1 };
      if (null != serialPort.mFd) {
        serialPort.writeBytes(PrintAlignmentData);
      }
      byte[] PrintCodeAlignmentData = { 29, 80, 1 };
      if (null != serialPort.mFd) {
        serialPort.writeBytes(PrintCodeAlignmentData);
      }
      byte[] PrintCodeData = { 29, 119, 2, 29, 104, 120, 29, 102, 0, 29, 72, 0, 29, 107, 73, 13, 123, 65, 49, 49, 49, 49, 49, 49, 49, 49, 50, 51, 49 };
      if (null != serialPort.mFd) {
        serialPort.writeBytes(PrintCodeData);
      }
      Thread.sleep(1000L);
      


      byte[] PrintPaperData = { 27, 100, 8 };
      if (null != serialPort.mFd) {
        serialPort.writeBytes(PrintPaperData);
      }
      byte[] cutPaperData = { 27, 105 };
      if (null != serialPort.mFd) {
        serialPort.writeBytes(cutPaperData);
      }
    }
    catch (Exception ex)
    {
      LogUtil.writePrintInfo("打印内容异常:" + ex.getMessage());
      return -1;
    }
    return 0;
  }
  
  public static int printSelfCheck()
  {
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] cutPaperData = { 29, 40, 65, 2, 0, 0, 2 };
      if (null != serialPort.mFd) {
        serialPort.writeBytes(cutPaperData);
      }
    }
    catch (Exception ex)
    {
      LogUtil.writePrintInfo("打印内容异常:" + ex.getMessage());
      return -1;
    }
    return 0;
  }
}
